<template>
	<view class="container">
		<view class="votebox" v-if="datalist.length">
			<navigator class="voteitem bgWhite radius5 plr15 mb15" v-for="(item,index) in datalist" :key="index"
				:url="'/pages/voteResult/index?id='+item.id">
				<view class="title justify-between align-center">
					<view class="">
						{{item.title}}
					</view>
					<text class="status"> {{dataInfo.status==1?'投票中...':dataInfo.status==2?'本次投票已结束':'待开始' }} </text>
				</view>
				<view class="mt5">
					开始时间: {{item.starttime}}
				</view>
				<view class="mt5">
					截止时间: {{item.endtime}}
				</view>
				<view class="mt5">
					投票范围: 恒大名都
				</view>
				<view class="joininfo align-center justify-between">
					<text>{{item.num}}人参与</text>
					<view class="align-center share">
						<image src="/static/images/share.png" mode=""></image>分享
					</view>
				</view>
			</navigator>
		</view>

		<uv-empty v-if="datalist.length==0"></uv-empty>

		<uv-popup ref="popup" :close-on-click-overlay="true" bgColor="" mode="center" round="15">
			<view class="popcontent">
				<image src="../../static/images/hongbao.png" mode=""></image>
			</view>

		</uv-popup>
		<customTab :active="2"></customTab>
	</view>
</template>

<script>
	import customTab from '../../components/customTab.vue';
	let app = getApp();
	export default {
		components: {
			customTab
		},
		data() {
			return {
				act: 0,
				dataInfo: {},
				certificateNum: '',
				datalist: [],
				page: 1,
				total: 0
			}
		},
		onLoad(opt) {
			

		},
		onShow() {
			this.page = 1;
			this.datalist = []
			
			if (!uni.getStorageSync('logintoken')) {
				const code = this.getUrlParam('code');
				if (code) {
					this.exchangeToken(code);
				} else {
			
					this.wechatAuthLogin()
				}
			
			}else{
				this.$api.realInfo().then(res=>{
					console.log(res)
					if(res.data){
						this.getList()
					}else{
						uni.showToast({
							title:'未实名认证，请先实名认证',
							duration:1500,
							icon:"none",
							complete:function(){
								
							}
						})
							
						setTimeout(()=>{
							uni.navigateTo({
								url:'/pages/realName/index'
							})
						},1000)
					}
				})
			}
		},
		mounted() {
			// this.$refs.popup.open()

		},
		onReachBottom() {

			if (this.datalist.length < this.total) {
				this.page++;
				this.getList()
			}


		},
		methods: {
			wechatAuthLogin() {
				// 公众号APPID
				const appid = 'wx2f4f26898c0dfac1';
				// 授权后跳转的回调地址（需编码）
				let url = window.location.href
				const redirect_uri = encodeURIComponent(url);
				// 授权类型：snsapi_base（静默授权）或 snsapi_userinfo（需用户确认）
				const scope = 'snsapi_userinfo';
				// 重定向到微信授权页面
				console.log('111', url)
				window.location.href =
					`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}&response_type=code&scope=${scope}&state=STATE#wechat_redirect`;
			},
			getUrlParam(name) {
				const reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`);
				const r = window.location.search.substr(1).match(reg);
				if (r != null) return decodeURIComponent(r[2]);
				return null;
			},

			async exchangeToken(code) {
				try {
					const res = await this.$api.wechatLogin({
						code: code
					})

					if (res.data) {
						// 登录成功，保存token
						// uni.setStorageSync('logintoken', res.data.access_token);
						uni.setStorageSync('userInfo', res.data.userinfo)
						uni.setStorageSync('logintoken', res.data.userinfo.token)
						// 跳转到首页或原页面
						uni.reLaunch({
							url: '/pages/vote/index'
						});
					}
				} catch (error) {
					console.error('登录失败', error);
				}
			},
			getList() {
				this.$api.voteInfo().then(res => {
					console.log(res)
					this.total = res.data.total
					this.datalist = [...this.datalist, ...res.data.data]
				})
			}
		}





	}
</script>
<style scoped lang="scss">
	.container {
		height: 100vh;


	}

	.votebox {
		padding: 36rpx 30rpx;
	}

	.voteitem {
		padding-top: 40rpx;
		padding-bottom: 30rpx;
		font-size: 28rpx;
		color: #9B9BA2;

		.title {

			view {
				font-weight: 800;
				font-size: 32rpx;
				color: #000000;
			}

			.status {
				font-weight: 500;
				font-size: 26rpx;
				color: #2AB70E;
			}

		}

		.joininfo {
			margin-top: 20rpx;
			border-top: 1px solid rgba(217, 215, 218, .3);
			padding-top: 20rpx;

			image {
				width: 26rpx;
				height: 26rpx;
			}

			.share {
				color: rgba(62, 89, 242, 1);
			}
		}
	}

	.popcontent {
		position: relative;
		width: 698rpx;
		height: 910rpx;

		image {
			position: absolute;
			width: 698rpx;
			height: 910rpx;
		}
	}
</style>